const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        qrcode:'',
        showposter:false,
        share:false,     
        cityIndex:0,
        login:false,
        user:{'role':0}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        console.log('------mine-------');
        console.log(options);
        console.log('-------mine------');
        var parent_id=options.parent_id;
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var open=wx.getStorageSync('open');
        console.log(open);
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:mine,options:"+JSON.stringify(options));
        console.log(open)
        log.info("Time:"+date+",page:mine,open:"+JSON.stringify(open));
        if (open === null){
          console.log('open is null')
          wx.navigateTo({
            url: '../login/index?parent_id='+parent_id,
          })
        }
        if(open == ""){
            console.log('open is null')
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }else{
            console.log('--mine----')
            console.log(open)
            console.log('----mine---')
        }
        wx.showShareMenu({
            menus: ['shareAppMessage'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })
          wx.hideShareMenu({
              menus:['shareTimeline']
          })     
        that.setData({
            platform:app.globalData.platform,    
            open:open,
             width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain
          })
    },
    getIndex:function(){
        let that = this; 
        var open = wx.getStorageSync('open');
               
        wx.request({
          url: app.globalData.domain+'wechat/mine/getIndex',
          data:{from:1,open_id:open.id},
          success:res=>{
              var data=res.data;
              console.log(data);
              if(res.statusCode ==200){            
                that.setData({
                  mine:data.mine,
                  car:data.car,
                  open:data.open,
                  is_sign:data.is_sign
                })
              }else{
                  this.showModals('系统错误')
              }
         
          }
      })
    },
    goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
              phoneNumber: '400-800-2177',
            })
        }else if(index=='index'){
            wx.reLaunch({
              url: url,
            })
        }else{
            wx.navigateTo({
              url: url,
            })
        }
        console.log(url);
    },
    showModals:function(msg,url,index){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
              console.log(res)
               if(res.confirm){
                if(url){
                  if(index == 'index'){
                    wx.reLaunch({
                      url: url,
                    })
                  }else{
                    wx.navigateTo({
                      url: url,
                    })
                  }
                }
               }
               if(res.cancel){
    
               }
            }
        })
    },
    gotoLogin:function(){
        this.setData({
            login:true
        })
    },
    cancelLogin:function(){
        this.setData({
            login:false
        })
    },
    getPhoneNumber (e){
        let that=this;
        var open=wx.getStorageSync('open');
        var date=util.formatTime(new Date());   
        log.info("Time:"+date+",page:mine,open:"+JSON.stringify(open));
        var parent_id=wx.getStorageSync('parent_id');
        if (open === null){
          console.log('open is null')
          wx.navigateTo({
            url: '../login/index?parent_id='+parent_id,
          })
        }
        if(open == ""){
          console.log('open is null')
          wx.navigateTo({
            url: '../login/index?parent_id='+parent_id,
          })
      }
        console.log(e);       
                wx.request({
                    url:app.globalData.domain+'wechat/login/getPhone',
                    data:{
                      code:e.detail.code,
                       id:open.id,
                       type:3
                    },
                    method:'GET',
                    header:{
                        'content-type':'application/json'
                    },
                    success:function(res){
                      console.log(res)
                        //wx.setStorageSync('PhoneNumber',res.data.phone);
                        if(res.data.code==200){
                          open.phone = res.data.phone;
                          wx.setStorageSync('open', open);
                          that.setData({
                              open:open,
                              login:false
                          })
                          that.showModals('获取手机号成功');
                        }else{
                            that.showModals('获取手机号失败');
                        } 
                        //console.log("手机号为");
                        //console.log(res);
                    },
                    fail:function(err){
                        console.log(err);
                    }
                })
       
      },

    sign:function(){
        let that=this;
        var open=wx.getStorageSync('open'); 
        var sign=this.data.sign;
        if(sign==1){
          this.showModals('您今日已经签到');
        }else{
          wx.request({
            url: app.globalData.domain+'wechat/mine/sign',
            data:{from:1,open_id:open.id,score:5},
            success:res=>{
                console.log(res);
                var data=res.data;
                if(res.statusCode==200){
                    that.setData({
                        is_sign:1
                    })
                    that.showModals(data.msg,'#','#');
                }else{
                    that.showModals('系统错误','#','#')
                }
            }
        })
        }
       
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {       
        var open=wx.getStorageSync('open');
        this.setData({
            open:open
        })
        this.getIndex();

    },
    createPoster(){
        var open=wx.getStorageSync('open');
    let that=this;
    var open_id=open.id;
    if(open_id){
      wx.request({
        url: app.globalData.domain+'wechat/mine/poster',
      data:{open_id:open_id,from:3},
      success:function(res){
        console.log(res);
        var statusCode=res.statusCode;
        if(statusCode==200){
            var qrcodeUrl=app.globalData.uploadUrl+res.data.qrcode;
            console.log(qrcodeUrl);
            that.setData({
             showposter:true,
              qrcode:qrcodeUrl
            }) 
        }else{
            that.showModals('网络错误');
        }
        

      },
      fail:function(e){
        console.log(e);
      }
      })

    }
    },
    closePoster(){
        this.setData({
            showposter:false
        })
    },
    save () {
        let that = this
        //若二维码未加载完毕，加个动画提高用户体验
        wx.showToast({
         icon: 'loading',
         title: '正在保存图片',
         duration: 1000
        })
        //判断用户是否授权"保存到相册"
        wx.getSetting({
         success (res) {
          //没有权限，发起授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
           wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success () {//用户允许授权，保存图片到相册
             that.savePhoto();
            },
            fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
             wx.openSetting({
              success () {
               wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success() {
                 that.savePhoto();
                }
               })
              }
             })
            }
           })
          } else {//用户已授权，保存到相册
           that.savePhoto()
          }
         }
        })
       },
      //保存图片到相册，提示保存成功
       savePhoto() {
        let that = this
        var qrcode=this.data.qrcode;
        wx.downloadFile({
         url: qrcode,
         success: function (res) {
          wx.saveImageToPhotosAlbum({
           filePath: res.tempFilePath,
           success(res) {
            wx.showToast({
             title: '保存成功',
             icon: "success",
             duration: 1000
            })
           }
          })
         }
        })
       },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    setShare:function(id,md5){
        var open=wx.getStorageSync('open');
        var date=new Date();
        var time=date.getTime();       
        var open_id=open.id;
        
        wx.request({
            url: app.globalData.domain+'wechat/share/index',
            data:{open_id:open_id,from:3,id:id,page:'mine',sn:md5},
            success:function(res){
                console.log(res);
            }
        }) 
   
    
        console.log('setShare');
        //var md5=;

    },
    /* 分享朋友圈 */
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var open=wx.getStorageSync('open');
        var parent_id=open.id;
        var date=new Date();
        var time=date.getTime();
        var page='mine';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();  
        var day=date.getDate();  
        var md5=day+'M'+hour+'in'+second+'e';
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         this.setShare(1,md5);
         return{
           title: title,                 
           query: 'parent_id='+parent_id+'&sn='+md5+'&from=share',        
           imageUrl:shareLogo      
           }
       },
    hiddenShare:function(){
        this.setData({
          share:false
        })
      },
      /* 分享好友 */  
    onShareAppMessage(e) {
        var open=wx.getStorageSync('open');
        var parent_id=open.id;
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
        var date=new Date();
        var time=date.getTime();
        var page='mine';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();    
        var day=date.getDate();  
        var md5=day+'M'+hour+'in'+second+'e';
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?parent_id='+parent_id+'&sn='+md5+'&from=share',   
   　　　　imageUrl: shareLogo 
   　　　
   　　}

   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?parent_id='+parent_id+'&sn='+md5+'&from=share';
   　　}
   this.setShare(2,md5);
   　　// 返回shareObj
   　　return shareObj;
    }
})